//
//  MSPasswordView.m
//  JYG_Agent
//
//  Created by koki on 15/10/15.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "MSPasswordView.h"

#define kDotCount 6
#define kDotSize CGSizeMake (10.0f,10.0f)

@interface MSPasswordView ()<UITextFieldDelegate>
@property   NSMutableArray *indicatorArrary;
@property   NSMutableArray *sepLineArrary;
@end

@implementation MSPasswordView
@synthesize indicatorArrary,sepLineArrary;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initView];
    }
    return self;
}

- (void)initView
{
    self.backgroundColor = [UIColor whiteColor];
    indicatorArrary = [[NSMutableArray alloc] init];
    sepLineArrary= [[NSMutableArray alloc] init];
    _passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    _passwordTextField.hidden = YES;
    _passwordTextField.delegate = self;
    _passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _passwordTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self addSubview:_passwordTextField];
    
    CGFloat width = self.width / kDotCount;
    for (int i = 0; i < (kDotCount-1); i++)
    {
        UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake((i + 1) * width, 0,0.5, self.height)];
        lineImageView.backgroundColor = [UIColor grayColor];
        [self addSubview:lineImageView];
        [sepLineArrary addObject:lineImageView];
    }
    
    for (int i = 0; i < kDotCount; i++)
    {
        UIImageView *dotImageView = [[UIImageView alloc] initWithFrame:CGRectMake((width - kDotSize.width) / 2.0f + i * width, (self.height - kDotSize.height) / 2.0f, kDotSize.width, kDotSize.height)];
        dotImageView.backgroundColor = [UIColor blackColor];
        dotImageView.layer.cornerRadius = kDotSize.width / 2.0f;
        dotImageView.clipsToBounds = YES;
        dotImageView.hidden = YES;
        [self addSubview:dotImageView];
        [indicatorArrary addObject:dotImageView];
    }
    
    [[NSNotificationCenter defaultCenter] addObserverForName:UITextFieldTextDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){
        if ([note.object isEqual:_passwordTextField])
        {
            [self setDotWithCount:_passwordTextField.text.length];
            
        }}];
    
    self.layer.borderColor=[UIColor grayColor].CGColor;
    self.layer.borderWidth=0.5;
    self.layer.cornerRadius=3;
}

-(void)setBoardColor:(UIColor *)boardColor
{
    _boardColor=boardColor;
    self.layer.borderColor=boardColor.CGColor;
    for (UIImageView *lineImageView in sepLineArrary)
    {
        lineImageView.backgroundColor = boardColor;
    }
}

- (void)dealloc
{
    self.indicatorArrary=nil;
    self.sepLineArrary=nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    if([string isEqualToString:@"\n"])
    {
        [textField resignFirstResponder];
        return NO;
    }
    else if(string.length == 0)
    {
        return YES;
    }
    else if(textField.text.length >= kDotCount)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

-(void)becomeFirstResponder
{
    [_passwordTextField becomeFirstResponder];
}


- (void)setDotWithCount:(NSInteger)count
{
    for (UIImageView *dotImageView in indicatorArrary)
    {
        dotImageView.hidden = YES;
    }
    
    for (int i = 0; i< count; i++)
    {
        ((UIImageView*)[indicatorArrary objectAtIndex:i]).hidden = NO;
    }
    
    if (self.delegate!=nil &&[self.delegate respondsToSelector:@selector(passwordViewInput:)])
    {
        [self.delegate passwordViewInput:self];
    }
}

- (void)clearUpPassword
{
    _passwordTextField.text = @"";
    [self setDotWithCount:_passwordTextField.text.length];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_passwordTextField becomeFirstResponder];
}

@end
